package com.ruoyi.system.service.impl;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

import com.ruoyi.system.constant.Constants;
import com.ruoyi.system.domain.AllTask;
import com.ruoyi.system.domain.MriqcConfig;
import com.ruoyi.system.domain.TaskType;
import com.ruoyi.system.mapper.AllTaskMapper;
import com.ruoyi.system.mapper.TaskTypeMapper;
import com.ruoyi.system.utils.OutStream;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IstrokeConfigMapper;
import com.ruoyi.system.domain.IstrokeConfig;
import com.ruoyi.system.service.IIstrokeConfigService;

import static com.ruoyi.system.utils.Util.getsub;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-14
 */
@Service
public class IstrokeConfigServiceImpl implements IIstrokeConfigService
{
    @Autowired
    private IstrokeConfigMapper istrokeConfigMapper;
    @Autowired
    private AllTaskMapper allTaskMapper;
    @Autowired
    private TaskTypeMapper taskTypeMapper;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public IstrokeConfig selectIstrokeConfigById(Long id)
    {
        return istrokeConfigMapper.selectIstrokeConfigById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param istrokeConfig 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<IstrokeConfig> selectIstrokeConfigList(IstrokeConfig istrokeConfig)
    {
        return istrokeConfigMapper.selectIstrokeConfigList(istrokeConfig);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param istrokeConfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertIstrokeConfig(IstrokeConfig istrokeConfig)
    {
        return istrokeConfigMapper.insertIstrokeConfig(istrokeConfig);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param istrokeConfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIstrokeConfig(IstrokeConfig istrokeConfig)
    {
        return istrokeConfigMapper.updateIstrokeConfig(istrokeConfig);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIstrokeConfigByIds(Long[] ids)
    {
        return istrokeConfigMapper.deleteIstrokeConfigByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIstrokeConfigById(Long id)
    {
        return istrokeConfigMapper.deleteIstrokeConfigById(id);
    }

    @Override
    public IstrokeConfig selectNew(IstrokeConfig istrokeConfig) {
        return istrokeConfigMapper.selectIstrokeConfigListNew(istrokeConfig);
    }

    @Override
    public void runstatus2to3() {
        AllTask allTask = new AllTask();
        allTask.setStatus(2l);
        allTask.setTypeId(2l);  //typeid为2 istroke任务
        List<AllTask> allTasks = allTaskMapper.selectAllTaskList(allTask);
        for(AllTask at_istroke:allTasks){
            at_istroke.setStatus(3l);
            IstrokeConfig istrokeConfigtemp = new IstrokeConfig();
            istrokeConfigtemp.setAllTaskId(at_istroke.getId());
            IstrokeConfig istrokeConfig = istrokeConfigMapper.selectIstrokeConfigListNew(istrokeConfigtemp);
            int count = getsub(IstrokeConfig.getSubject()).size();
            int min=0;
            TaskType taskType = taskTypeMapper.selectTaskTypeById(at_istroke.getTypeId());
            min = (int) (count*taskType.getPertime());
            LocalDateTime currentDateTime = LocalDateTime.now();
            LocalDateTime newDateTime = currentDateTime.plusMinutes(min);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = newDateTime.format(formatter);
            at_istroke.setOverTime(formattedDateTime);
            allTaskMapper.updateAllTask(at_istroke);
        }

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        forkJoinPool.submit(() ->
                allTasks.parallelStream().forEach(at_istroke -> {
                    IstrokeConfig istrokeConfigtemp =  new IstrokeConfig();
                    istrokeConfigtemp.setAllTaskId(at_istroke.getId());
                    IstrokeConfig istrokeConfig = istrokeConfigMapper.selectIstrokeConfigListNew(istrokeConfigtemp);
                    AllTask allTask1 = allTaskMapper.selectAllTaskById(at_istroke.getId());
                    String task = allTask1.getId().toString();
                    String subjectsList = istrokeConfig.getSubject();
                    Integer inputype = Math.toIntExact(istrokeConfig.getInputType());
                    String modal = "";
                    if(inputype==1)
                        modal="T1";
                    else if(inputype==2)
                        modal="DWI";

                    Process proc;
                    try {
                        String workingdic = Constants.istrokepth;


                        String py = "python /root/web/segment/"+"istroke.py --WorkingDir=\"" + workingdic + "\" --subjects=\""+subjectsList+
                                "\" --taskid=\""+task + "\" --modal=\"" + modal +"\"";

                        System.out.println("-----------istroke------------"+py);

                        proc = Runtime.getRuntime().exec(py);// 执行py文件

                        OutStream error= new OutStream(proc.getErrorStream(),"Error");
                        OutStream output= new OutStream(proc.getInputStream(),"OutPut");
                        error.start();
                        output.start();
                        proc.waitFor(); //等待子进程结束

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                })
        ).join();
        forkJoinPool.shutdown();
    }
}
